/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mrp.productionPlanning.entity.DailyDetailEntity;
import mrp.productionPlanning.entity.DailyMPSTableEntity;
import mrp.productionPlanning.entity.MPSTableEntity;
import mrp.productionPlanning.entity.MonthlyDetailEntity;
import scm.inventoryControl.session.stateless.InventoryControlSessionBean;

/**
 *
 * @author xhx
 */
@Stateless
@LocalBean
public class DailyMPSTableSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    EntityManager em;
    @EJB
    private MonthlyDetailSessionBean mdsb = new MonthlyDetailSessionBean();
        @EJB
    private InventoryControlSessionBean icsb = new InventoryControlSessionBean();
//    @EJB
//    private InventoryControlSessionBean icsb = new InventoryControlSessionBean();
    @EJB
    private MPSTableSessionBean mpsTableSessionBean = new MPSTableSessionBean();
    @EJB
    private DailyDetailSessionBean dailyDetailSessionBean = new DailyDetailSessionBean();

    public DailyMPSTableEntity createMPSTableA(Calendar c) {

        DailyMPSTableEntity dailyMpsTableA = new DailyMPSTableEntity();
//        dailyMpsTableA.setDay(c.getTime());
        Date d = c.getTime();
        
//        System.out.print("你妹......." + d.toString());
        
        DailyDetailEntity dailyDetailEntity = new DailyDetailEntity();
        dailyDetailEntity = dailyDetailSessionBean.getDailyDetailByDate(d);
        MonthlyDetailEntity monthlyDetail = mdsb.getMonthlyDetailByDateForDaily(d);
        List<DailyDetailEntity> listOfDays = sortList(monthlyDetail.getDays());
        MPSTableEntity mPSTableEntity = mpsTableSessionBean.getMPSTableByDate(d, "productA");
        int currentInventory = mPSTableEntity.getInventory();
        
      
        int day_forecastAmount = dailyDetailEntity.getScheduleA();
        System.out.println(".... " + day_forecastAmount);
        if(c.getTime().equals(listOfDays.get(0).getDate())){
        int dailyInventoryLevel = currentInventory - day_forecastAmount + dailyDetailEntity.getScheduleA();
        dailyMpsTableA.setInventory(dailyInventoryLevel);
        }
        else{ 
        DailyDetailEntity lastDailyDetail = listOfDays.get(listOfDays.indexOf(dailyDetailEntity)-1);
        Date lastDay = lastDailyDetail.getDate();
//        System.out.println("你没2......" + lastDay.toString());
        DailyMPSTableEntity lastDaily = getDailyMPSTableEntityByDate(lastDay, "productA");
        int dailyInventoryLevel = lastDaily.getInventory() - day_forecastAmount + dailyDetailEntity.getScheduleA();
        dailyMpsTableA.setInventory(dailyInventoryLevel);
        }
        dailyMpsTableA.setDailyDetailEntity(dailyDetailEntity);

        Timestamp ts = new Timestamp(d.getTime());
        dailyMpsTableA.setTs(ts);

        String monthName = convertToMonth(c.get(Calendar.MONTH));
        int yearName = c.get(Calendar.YEAR);
        int currentDay = c.get(Calendar.DAY_OF_MONTH);
        System.out.print("**************Test Date " + currentDay);

        dailyMpsTableA.setMonthlyDetailEntity(monthlyDetail);
        dailyMpsTableA.setDay(dailyDetailEntity.getDate());
        dailyMpsTableA.setCurrentDay(currentDay);
        dailyMpsTableA.setMonthName(monthName);
        dailyMpsTableA.setYearName(yearName);
        dailyMpsTableA.setProductType("productA");
        dailyMpsTableA.setProductionDemand(day_forecastAmount);
        
        
        em.persist(dailyMpsTableA);
        em.flush();

        return dailyMpsTableA;
    }

    public DailyMPSTableEntity createMPSTableB(Calendar c) {
        DailyMPSTableEntity dailyMpsTableB = new DailyMPSTableEntity();
//        dailyMpsTableB.setDay(c.getTime());
        Date d = c.getTime();
       
        DailyDetailEntity dailyDetailEntity = new DailyDetailEntity();
        dailyDetailEntity = dailyDetailSessionBean.getDailyDetailByDate(d);

        MonthlyDetailEntity monthlyDetail = mdsb.getMonthlyDetailByDateForDaily(d);
        List<DailyDetailEntity> listOfDays = sortList(monthlyDetail.getDays());
        MPSTableEntity mPSTableEntity = mpsTableSessionBean.getMPSTableByDate(d, "productB");
//        int currentInventory = icsb.getCurrentInventoryLevel("Classic Nutri Bar-Fruits");
        int currentInventory = mPSTableEntity.getInventory();
        
        int day_forecastAmount = dailyDetailEntity.getScheduleB();
        if(c.getTime().equals(listOfDays.get(0).getDate())){
        int dailyInventoryLevel = currentInventory - day_forecastAmount + dailyDetailEntity.getScheduleB();
        dailyMpsTableB.setInventory(dailyInventoryLevel);
        }
        else{
        DailyDetailEntity lastDailyDetail = listOfDays.get(listOfDays.indexOf(dailyDetailEntity)-1);
        Date lastDay = lastDailyDetail.getDate();
        DailyMPSTableEntity lastDaily = getDailyMPSTableEntityByDate(lastDay, "productB");
        int dailyInventoryLevel = lastDaily.getInventory() - day_forecastAmount + dailyDetailEntity.getScheduleB();
        dailyMpsTableB.setInventory(dailyInventoryLevel);
        }
        dailyMpsTableB.setDailyDetailEntity(dailyDetailEntity);

        Timestamp ts = new Timestamp(d.getTime());
        dailyMpsTableB.setTs(ts);

        String monthName = convertToMonth(c.get(Calendar.MONTH));
        int yearName = c.get(Calendar.YEAR);
        int currentDay = c.get(Calendar.DAY_OF_MONTH);

        dailyMpsTableB.setMonthlyDetailEntity(monthlyDetail);
        dailyMpsTableB.setDay(dailyDetailEntity.getDate());
        dailyMpsTableB.setCurrentDay(currentDay);
        dailyMpsTableB.setMonthName(monthName);
        dailyMpsTableB.setYearName(yearName);
        dailyMpsTableB.setProductType("productB");
        dailyMpsTableB.setProductionDemand(day_forecastAmount);

        em.persist(dailyMpsTableB);
        em.flush();



        return dailyMpsTableB;
    }

    public DailyMPSTableEntity createMPSTableC(Calendar c) {
        DailyMPSTableEntity dailyMpsTableC = new DailyMPSTableEntity();
//        dailyMpsTableC.setDay(c.getTime());
        Date d = c.getTime();
        
        DailyDetailEntity dailyDetailEntity = new DailyDetailEntity();
        dailyDetailEntity = dailyDetailSessionBean.getDailyDetailByDate(d);

        MonthlyDetailEntity monthlyDetail = mdsb.getMonthlyDetailByDateForDaily(d);
        List<DailyDetailEntity> listOfDays = sortList(monthlyDetail.getDays());
        MPSTableEntity mPSTableEntity = mpsTableSessionBean.getMPSTableByDate(d, "productC");
//        int currentInventory = icsb.getCurrentInventoryLevel("Premium Power Bar-Chocolate Nuts");
//        int day_month = c.get(Calendar.DAY_OF_MONTH);
        int currentInventory = mPSTableEntity.getInventory();
        int day_forecastAmount = dailyDetailEntity.getScheduleC();
        if(c.getTime().equals(listOfDays.get(0).getDate())){
        int dailyInventoryLevel = currentInventory - day_forecastAmount + dailyDetailEntity.getScheduleC();
        dailyMpsTableC.setInventory(dailyInventoryLevel);
        }
        else{
        DailyDetailEntity lastDailyDetail = listOfDays.get(listOfDays.indexOf(dailyDetailEntity)-1);
        Date lastDay = lastDailyDetail.getDate();
        DailyMPSTableEntity lastDaily = getDailyMPSTableEntityByDate(lastDay, "productC");
        int dailyInventoryLevel = lastDaily.getInventory() - day_forecastAmount + dailyDetailEntity.getScheduleC();
        dailyMpsTableC.setInventory(dailyInventoryLevel);
        }
        dailyMpsTableC.setDailyDetailEntity(dailyDetailEntity);

        Timestamp ts = new Timestamp(d.getTime());
        dailyMpsTableC.setTs(ts);

        String monthName = convertToMonth(c.get(Calendar.MONTH));
        int yearName = c.get(Calendar.YEAR);
        int currentDay = c.get(Calendar.DAY_OF_MONTH);

        dailyMpsTableC.setMonthlyDetailEntity(monthlyDetail);
        dailyMpsTableC.setDay(dailyDetailEntity.getDate());
        dailyMpsTableC.setCurrentDay(currentDay);
        dailyMpsTableC.setMonthName(monthName);
        dailyMpsTableC.setYearName(yearName);
        dailyMpsTableC.setProductType("productC");
        dailyMpsTableC.setProductionDemand(day_forecastAmount);
        em.persist(dailyMpsTableC);
        em.flush();


        return dailyMpsTableC;
    }

    public DailyMPSTableEntity createMPSTableD(Calendar c) {
        DailyMPSTableEntity dailyMpsTableD = new DailyMPSTableEntity();
//        dailyMpsTableD.setDay(c.getTime());
        Date d = c.getTime();
        DailyDetailEntity dailyDetailEntity = new DailyDetailEntity();
        dailyDetailEntity = dailyDetailSessionBean.getDailyDetailByDate(d);
        MonthlyDetailEntity monthlyDetail = mdsb.getMonthlyDetailByDateForDaily(d);
        List<DailyDetailEntity> listOfDays = sortList(monthlyDetail.getDays());
        MPSTableEntity mPSTableEntity = mpsTableSessionBean.getMPSTableByDate(d, "productD");
//       int currentInventory = icsb.getCurrentInventoryLevel("Premium Power Bar-Redcurrant Fruits");
//        int day_month = c.get(Calendar.DAY_OF_MONTH);
        int currentInventory = mPSTableEntity.getInventory();
        int day_forecastAmount = dailyDetailEntity.getScheduleD();
        if(c.getTime().equals(listOfDays.get(0).getDate())){
        int dailyInventoryLevel = currentInventory - day_forecastAmount + dailyDetailEntity.getScheduleD();
        dailyMpsTableD.setInventory(dailyInventoryLevel);
        }
        else{
        DailyDetailEntity lastDailyDetail = listOfDays.get(listOfDays.indexOf(dailyDetailEntity)-1);
        Date lastDay = lastDailyDetail.getDate();
        DailyMPSTableEntity lastDaily = getDailyMPSTableEntityByDate(lastDay, "productD");
        int dailyInventoryLevel = lastDaily.getInventory() - day_forecastAmount + dailyDetailEntity.getScheduleD();
        dailyMpsTableD.setInventory(dailyInventoryLevel);
        }
        dailyMpsTableD.setDailyDetailEntity(dailyDetailEntity);
        
        Timestamp ts = new Timestamp(d.getTime());
        dailyMpsTableD.setTs(ts);

        String monthName = convertToMonth(c.get(Calendar.MONTH));
        int yearName = c.get(Calendar.YEAR);
        int currentDay = c.get(Calendar.DAY_OF_MONTH);
        
        dailyMpsTableD.setMonthlyDetailEntity(monthlyDetail);
        dailyMpsTableD.setDay(dailyDetailEntity.getDate()); 
        dailyMpsTableD.setCurrentDay(currentDay);
        dailyMpsTableD.setMonthName(monthName);
        dailyMpsTableD.setYearName(yearName);
        dailyMpsTableD.setProductType("productD");
        dailyMpsTableD.setProductionDemand(day_forecastAmount);
        em.persist(dailyMpsTableD);
        em.flush();
        return dailyMpsTableD;
    }

    private String convertToMonth(int in) {
        String out = "";
        if (in == 0) {
            out = "Jan";
        } else if (in == 1) {
            out = "Feb";
        } else if (in == 2) {
            out = "Mar";
        } else if (in == 3) {
            out = "Apr";
        } else if (in == 4) {
            out = "May";
        } else if (in == 5) {
            out = "Jun";
        } else if (in == 6) {
            out = "Jul";
        } else if (in == 7) {
            out = "Aug";
        } else if (in == 8) {
            out = "Sep";
        } else if (in == 9) {
            out = "Oct";
        } else if (in == 10) {
            out = "Nov";
        } else if (in == 11) {
            out = "Dec";
        }
        return out;
    }
    public List<DailyDetailEntity> sortList(List<DailyDetailEntity> input){
       
       Collections.sort(input, new Comparator<DailyDetailEntity>(){
        public int compare(DailyDetailEntity d1, DailyDetailEntity d2){
            return d1.getDate().compareTo(d2.getDate());
        }
        });
        return input;
    }
   
    public List<DailyMPSTableEntity> getAllDailyMPSTableEntity() {
        Query query = em.createQuery("SELECT u FROM DailyMPSTableEntity u");
        return query.getResultList();
    }
    public List<DailyMPSTableEntity> getAllDailyMPSTableEntityByMonth(String productType, String monthName,int yearName){
        Query query =em.createQuery("SELECT u FROM DailyMPSTableEntity u WHERE u.productType = :type AND u.monthName=:month AND u.yearName=:year");
        query.setParameter("type", productType);
        query.setParameter("month", monthName);
        query.setParameter("year",yearName);
        return query.getResultList(); 
    }
    public DailyMPSTableEntity getDailyMPSTableEntityByDate(Date d, String productType){
//        System.out.print("Inside Test 你妹...." + d.toString());
        Query query =em.createQuery("SELECT u FROM DailyMPSTableEntity u WHERE u.day =:dd AND u.productType = :type");
        query.setParameter("dd", d);
        query.setParameter("type", productType);
       return (DailyMPSTableEntity)query.getSingleResult();
       
    }

    public DailyMPSTableEntity getDailyMPSTableEntityById(Long id) {
        return em.find(DailyMPSTableEntity.class, id);
    }

    public void saveChanges(DailyMPSTableEntity dailyMPSTableEntity) {
        em.merge(dailyMPSTableEntity);
    }

    public void remove(DailyMPSTableEntity dailyMPSTableEntity) {
        em.remove(dailyMPSTableEntity);
    }
}
